/*
 * Description: 字符类型转换器
 */
package com.slipper.common.excel.parse.support;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;

import com.slipper.common.excel.ExcelColumn;

import java.nio.charset.StandardCharsets;

public class StringParse extends CellParse {

    @Override
    public Object parse(Cell cell, ExcelColumn column) throws Exception {
        String value = super.parserCellValue(cell, column);
        if (cell.getCellType() == CellType.NUMERIC) {
            int indexE = value.lastIndexOf("E");
            if (indexE >= 0) {
                //多个数字组成的字符串如，部门编码4512024919--在excel中会专场4.512024919E9-科学记数法
                value = value.substring(0,indexE).replace(".","");
            } else if (value.contains(".")) {
                value = value.substring(0, value.lastIndexOf("."));
            }
        } else {
            value = cell.getStringCellValue();
        }
        if (value != null
                && column.length() != 0
                && value.getBytes(StandardCharsets.UTF_8).length > column.length()) {
            throw new Exception("字符(一个汉字算2个字符)串最大长度应小于等于：" + column.length() + "！");
        } else if (value != null
                && column.elength() != 0
                && value.getBytes(StandardCharsets.UTF_8).length != column.elength()) {
            throw new Exception("字符(一个汉字算2个字符)串最大长度必须等于：" + column.elength() + "！");
        } else {
            return value;
        }
    }
}
